﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using System.Text;

public partial class witness : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    //見證分享分類 NodeId=8
    private int m_ParentNodeId = 8;
    private int m_NodeId;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_NodeId = QueryStringHelper.GetInteger("node", 0);

        if (!IsPostBack)
        {
            FillWitness();
            FillType();
        }
    }

    private void FillType()
    {
        IList<NodeVO> list = m_PostService.GetNodeListByParentId(m_ParentNodeId);
        StringBuilder sb = new StringBuilder();

        if (list != null && list.Count > 0)
        {
            foreach (NodeVO nodeVO in list)
            {
                sb.AppendFormat("<li class='Service-Slect-LI'><a href='witness.aspx?node={0}' class='whitebtn'>{1}</a></li>", nodeVO.NodeId, nodeVO.Name);
            }

            ltlType.Text = sb.ToString();
        }
    }

    private void FillWitness()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        if (m_NodeId <= 0)
        {
            conditions.Add("ParentNodeId", m_ParentNodeId.ToString());
        }
        else
        {
            NodeVO nodeVO = m_PostService.GetNodeById(m_NodeId);
            if (nodeVO != null)
            {
                ltlTitle1.Text = string.Format("-{0}",nodeVO.Name);
                ltlTitle2.Text = string.Format(">{0}", nodeVO.Name);
            }
            conditions.Add("NodeId", m_NodeId.ToString());
        }
        conditions.Add("Flag", "1");

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        RepeaterWitness.DataSource = m_PostService.GetPostList(conditions);
        RepeaterWitness.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        FillWitness();
    }

    protected void RepeaterWitness_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;
            postVO = m_PostService.GetPostByIdNoLazy(postVO.PostId);

            //標題
            //int customField1Length = 18;
            //if (!string.IsNullOrEmpty(postVO.CustomField1))
            //{
            //    string title = postVO.CustomField1;
            //    if (title.Length > customField1Length)
            //    {
            //        title = title.Substring(0, customField1Length) + "...";
            //    }

            //    UIHelper.SetLiteralText(ctrl, "ltlCustomField1", title);
            //}

            UIHelper.SetLiteralText(ctrl, "ltlNodeName", postVO.Node.Name);
        }
    }
}